<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final
 * @category    Controllers
 * @see 		Users
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Users extends CI_Controller {
	var $q = '';
	var $limit = '';
	var $orderby = '';
	var $ordermode = '';
	var $added = '';
	var $updated = '';
	var $permissed = '';
	var $failed = '';
	var $name = '';
	var $uri_arr = array();
	var $uri_str = '';
	var $user_id = 0;

	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (check_rights() == false)
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'User', true);
		$this->q = $this->input->get('q', true);
		$this->orderby = $this->input->get('orderby', true);
		$this->ordermode = $this->input->get('ordermode', true);
		$this->added = $this->input->get('added', true);
		$this->updated = $this->input->get('updated', true);
		$this->permissed = $this->input->get('permissed', true);
		$this->failed = $this->input->get('failed', true);
		$this->name = $this->input->get('name', true);
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'], 
			'q' => $this->q,
			'rowstart' => $GLOBALS['var']['rowstart'],
			'limit' => $this->limit,
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode
		);
		$this->uri_str = url_uri($this->uri_arr);
	}

	public function index()
	{
		/*
		* Xu ly du lieu
		*/
		$num_rows = $this->User->show_users($this->uri_arr, true);
		$page_list = page_list($num_rows, $this->uri_arr);
		$data = array(
			'q' => $this->q,
			'rowstart' => $GLOBALS['var']['rowstart'],
			'added' => $this->added,
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'uri_str' => $this->uri_str,
			'url_str' => site_url('users'),
			'users' => array(),
			'page_list' => $page_list,
			'action' => site_url($GLOBALS['var']['act'].'/process_group').$this->uri_str,
			'usergroups' => array()
		);
		$rows = $this->User->show_users($this->uri_arr);
		if ($rows) $data['users'] = $rows;
		$rows = $this->User->show_groups($this->uri_arr);
		if ($rows) $data['usergroups'] = $rows;
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Quản lý nhân sự',
			'add_link' => current_url().'/update',
			'search' => true,
			'page_list' => $page_list,
			'datetime_picker' => false,
			'submit_btn' => false,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/index', $data);
		$this->load->view('footer');
	}

	public function update($id = '')
	{
		/*
		* Kiem tra quyen cap nhap
		*/
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']) || ($id == 1 && $GLOBALS['user']['id'] != 1))
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Xu ly du lieu
		*/
		$info = $this->User->info($id);
		$data = array(
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'info' => $info,
			'usergroups' => $this->User->usergroups(),
			'branchs' => $this->User->branchs(),
			'positions' => $this->User->positions(),
			'parts' => $this->User->parts(),
			'action' => site_url($GLOBALS['var']['act'].'/process_user').$this->uri_str
		);
		/*
		* Hien thi
		*/
		$header = array(
			'title' => ($id ? 'Cập nhập thông tin' : 'Thêm mới nhân sự'),
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => true,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/update', $data);
		$this->load->view('footer');
	}

	public function process_group()
	{
		/*
		* Kiem tra POST method
		*/
		if(!$_POST)
		{
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$name_vn = $this->input->post('name_vn', true);
		if (!token_validation() || $name_vn == '')
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Them tham so url
		*/
		$this->uri_arr['name'] = str_replace('&', '', $name_vn);
		/*
		* Xu ly du lieu
		*/
		$id = $this->input->post('id', true);
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		$data = array(
			'name_vn' => $name_vn,
			'level' => $this->input->post('level', true),
			'date_added' => date(TIME_SQL),
            'date_modified' => date(TIME_SQL)
		);
		/*
		* Ghi du lieu
		*/
		if ($this->User->process_group($data, $id))
		{
			if ($id > 0)
			{
				$this->uri_arr['updated'] = 2;
			}
			else
			{
				$this->uri_arr['added'] = 2;
			}
		}
		else
		{
			$this->uri_arr['failed'] = 2;
		}
		/*
		* Chuyen huong
		*/
		redirect($GLOBALS['var']['act'].url_uri($this->uri_arr));
	}

	public function process_user()
	{
		/*
		* Kiem tra POST method
		*/
		if (!$_POST)
		{
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$username = $this->input->post('username', true);
		if (!token_validation() || !$username)
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Them tham so url
		*/
		$this->uri_arr['name'] = str_replace('&', '', $username);
		/*
		* Xu ly du lieu
		*/
		$id = $this->input->post('id', true);
		/*
		* Kiem tra quyen cap nhap
		*/
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		$data = array(
			'email' => $this->input->post('email', true),
			'phone' => $this->input->post('phone', true),
			'level' => $this->input->post('level', true),
			'fullname' => $this->input->post('fullname', true),
			'gioitinh' => $this->input->post('gioitinh', true),
			'coso' => $this->input->post('coso', true),
			'bophan' => $this->input->post('bophan', true),
			'chucvu' => $this->input->post('chucvu', true),
			'date_nghiviec' => $this->input->post('date_nghiviec', true),
			'date_vaolam' => $this->input->post('date_vaolam', true),
			'mode_right' => $this->input->post('mode_right', true),
			'anluong' => $this->input->post('anluong', true),
			'noisinh' => $this->input->post('noisinh', true),
			'diachi' => $this->input->post('diachi', true),
			'cmnd' => $this->input->post('cmnd', true),
			'date_cmnd' => $this->input->post('date_cmnd', true),
			'noicap_cmnd' => $this->input->post('noicap_cmnd', true),
			'date_sinh' => $this->input->post('date_sinh', true),
			'luong_cb' => str_replace(',', '', $this->input->post('luong_cb', true)),
			'phucap_trachnhiem' => str_replace(',', '', $this->input->post('phucap_trachnhiem', true)),
			'phucap_chuyencan' => str_replace(',', '', $this->input->post('phucap_chuyencan', true)),
			'phucap_congtac' => str_replace(',', '', $this->input->post('phucap_congtac', true)),
			'phucap_xang' => str_replace(',', '', $this->input->post('phucap_xang', true)),
			'muchoahong' => $this->input->post('muchoahong', true),
			'tien_bh' => str_replace(',', '', $this->input->post('tien_bh', true)),
			'mucthuong' => str_replace(',', '', $this->input->post('mucthuong', true)),
			'dkthuong' => str_replace(',', '', $this->input->post('dkthuong', true)),
			'username' => viet_decode($username),
			'viewall' => $this->input->post('viewall', true),
			'date_added' => date(TIME_SQL),
            'date_modified' => date(TIME_SQL)
		);
		if (!$id)
		{
			$data['user_rights'] = get_data('usergroups', 'id = "'.$data['level'].'"', 'group_rights');
		}
		$new_pass = $this->input->post('new_password', true);
	  	if ($new_pass != '') $data['password'] = md5($new_pass);
	  	/*
		* Ghi du lieu
		*/
	  	if ($this->User->process_user($data, $id))
	  	{
	  		$this->uri_arr['updated'] = 1;
	  	}
	  	else
	  	{
	  		$this->uri_arr['failed'] = 1;
	  	}
	  	/*
		* Chuyen huong
		*/
	  	if($id > 0)
	  	{
	  		redirect($GLOBALS['var']['act'].'/update/'.$id.url_uri($this->uri_arr));
	  	}
	  	else
	  	{
	  		redirect($GLOBALS['var']['act'].url_uri($this->uri_arr));
	  	}
	}

	public function permiss($id = '')
	{
		global $admin_groups;
		/*
		* Kiem tra id
		*/
		if(!$id)
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Xu ly du lieu
		*/
		$user = $this->User->info($id);
		if (!$user['id'])
		{
			redirect($GLOBALS['var']['act']);
		}
		$data = array(
			'permissed' => $this->permissed,
			'failed' => $this->failed,
			'name' => $this->name,
			'user' => $user,
			'rights' => explode('.', get_data('users', 'id = '.$id, 'user_rights')),
			'user_level' => get_data('users', 'id = '.$GLOBALS['var']['user_id'], 'level'),
			'all_rights' => array(),
			'action' => site_url($GLOBALS['var']['act'].'/rights').$this->uri_str
		);
		for ($i = 0; $i < count($GLOBALS['modules_groups']); $i++)
		{
			$rows = $this->User->get_rights($i);
			if ($rows) $data['all_rights'][$i] = $rows;
		}
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Phân quyền truy cập '.$user['username'],
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => true,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/permiss', $data);
		$this->load->view('footer');
	}

	public function rights()
	{
		/*
		* Kiem tra POST method
		*/
		if (!$_POST)
		{
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$id = $this->input->post('id', true);
		if (!token_validation() || !$id)
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Them tham so url
		*/
		$username = $this->input->post('username', true);
		$this->uri_arr['name'] = str_replace('&', '', $username);
		/*
		* Xu ly du lieu
		*/
		if ($id == 1)
		{
			redirect($GLOBALS['var']['act']);
		}
		$rights = $this->input->post('rights', true);
		$data = array(
			'user_rights' => json_encode($rights),
			'username' => $username,
			'date_modified' => date(TIME_SQL)
		);
		/*
		* Ghi du lieu
		*/
		if ($this->User->save_rights($data, $id))
		{
			$this->uri_arr['permissed'] = 1;
		}
		else
		{
			$this->uri_arr['failed'] = 1;
		}
		/*
		* Chuyen huong
		*/
		redirect($GLOBALS['var']['act'].'/permiss/'.$id.url_uri($this->uri_arr));
	}

	public function view($id = '')
	{
		/*
		* Kiem tra quyen cap nhap
		*/
		if (!$id)
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Xu ly du lieu
		*/
		$info = $this->User->info($id);
		if (empty($info['id']))
		{
			redirect($GLOBALS['var']['act']);
		}
		$data = array(
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'user' => $info,
			'usergroups' => $this->User->usergroups(),
			'positions' => $this->User->positions(),
			'parts' => $this->User->parts(),
			'action' => site_url($GLOBALS['var']['act'].'/process_user').$this->uri_str
		);
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Xem thông tin nhân viên',
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => true,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/view', $data);
		$this->load->view('footer');
	}

	public function permiss_group($id = '')
	{
		global $admin_groups;
		/*
		* Kiem tra id
		*/
		if(!$id)
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Xu ly du lieu
		*/
		$group = $this->User->group($id);
		if (!$group['id'])
		{
			redirect($GLOBALS['var']['act']);
		}
		$data = array(
			'permissed' => $this->permissed,
			'failed' => $this->failed,
			'name' => $this->name,
			'group' => $group,
			'rights' => explode('.', get_data('usergroups', 'id = "'.$id.'"', 'group_rights')),
			'all_rights' => array(),
			'action' => site_url($GLOBALS['var']['act'].'/rights_group').$this->uri_str
		);
		for ($i = 0; $i < count($GLOBALS['modules_groups']); $i++)
		{
			$rows = $this->User->get_rights($i);
			if ($rows) $data['all_rights'][$i] = $rows;
		}
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Phân quyền truy cập nhóm '.$group['name_vn'],
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => true,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/permiss_group', $data);
		$this->load->view('footer');
	}

	public function rights_group()
	{
		/*
		* Kiem tra POST method
		*/
		if (!$_POST)
		{
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$id = $this->input->post('id', true);
		if (!token_validation() || !$id)
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Them tham so url
		*/
		$name_vn = $this->input->post('name_vn', true);
		$this->uri_arr['name'] = str_replace('&', '', $name_vn);
		/*
		* Xu ly du lieu
		*/
		$rights = $this->input->post('rights', true);
		$data = array(
			'name_vn' => $name_vn,
			'group_rights' => json_encode($rights),
			'date_modified' => date(TIME_SQL)
		);
		/*
		* Ghi du lieu
		*/
		if ($this->User->save_rights_group($data, $id))
		{
			$this->uri_arr['permissed'] = 1;
		}
		else
		{
			$this->uri_arr['failed'] = 1;
		}
		/*
		* Chuyen huong
		*/
		redirect($GLOBALS['var']['act'].'/permiss_group/'.$id.url_uri($this->uri_arr));
	}
}

/* End of file users.php */
/* Location: ./application/controllers/users.php */
